/**
 * 载入项目引导文件
 */
require('./bootstrap');

window.Vue = require('vue');

//树形select选择框
import Treeselect from '@riophae/vue-treeselect'
// import the styles
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

//百度编辑器组件
Vue.component('ueditor', require('../components/Ueditor.vue'));
//单文件上传
Vue.component('WebuploaderSingle', require('../components/WebuploaderSingle.vue'));

let loadBox;//加载框

let news_empty = {
    'new_id':0,
    'title': '',
    'thumb_view_url': '',
    'author': '',
    'digest': '',
    'show_cover_pic': 1,
    'content': '',
    'content_source_url': '',
    'news_click_url': '',
    'need_open_comment': 1,
    'only_fans_can_comment': 1,
};
const app = new Vue({
    el: '#wrapper',
    data: {
        material_data: {},
        news_index:-1,
        news_cur_data:{},
        news_list: [],
        news_actions_index : -1,
        cat_list: page_params.catList,//分类选择项列表
        catNormalizer(node){
            return {
                id:node.cat_id,
                label:node.name,
                children: node.children,
            }
        },
    },
    computed: {
        display_material_data:function()//显示到前台的整体素材数据
        {
            if (_.isEmpty(this.material_data)){
                if (_.isEmpty(page_params.data_material) == false){
                    this.material_data = page_params.data_material;
                } else{
                    this.material_data = {
                        'material_id':0,
                        'cat_id': 0,
                        'is_forever': 1,
                    };
                }
            }
            return this.material_data;
        },
        display_new_list:function(){//显示到前台的整体文章列表
            //当图文列表为空的时候
            if (_.isEmpty(this.news_list)){
                if (_.isEmpty(page_params.data_new_list) == false){
                    this.news_list = page_params.data_new_list;
                }else{
                    this.news_list.push(news_empty);
                }
                this.news_index = 0;
            }
            return this.news_list;
        },
        news_count:function () {
            return this.news_list.length;
        },
    },
    watch: {
        news_index: function (val){
            //深度拷贝
            this.news_cur_data = _.cloneDeep(this.news_list[val]);
        },
        news_cur_data: function (val){
            Vue.set(this.news_list, this.news_index, val);
            //重新设置编辑器内容
            this.$refs.myEditor.setMyContent(this.news_cur_data.content);
        }
    },
    methods: {
        showActions:function (index){//显示操作层
            this.news_actions_index = index;
        },
        hideActions:function (index){//隐藏操作层
            this.news_actions_index = -1;
        },
        newsTrash: function (index) {//删除图文
            layer.confirm('你确定删除该篇文章么？', {icon: 3, title: '删除提示',shadeClose: true,}, (boxIndex) => {
                //如果删除当前编辑文章，则将文章索引先前一个移动
                if (index == this.news_index){
                    this.news_index = index - 1;
                }
                this.news_list.splice(index, 1);

                layer.close(boxIndex);
            });

        },
        newsMonveUp: function (index) {//上移图文
            if (index == 0){
                utils.showToastr({'status':'error','title':'移动错误','message':'顶级图文无法继续上移'});
                return false;
            }
            var tempOption = this.news_list[index - 1];
            Vue.set(this.news_list, index - 1, this.news_list[index]);
            Vue.set(this.news_list, index, tempOption);
            if (this.news_index == index){
                this.news_index = index - 1;
                this.news_actions_index = index - 1;
            }
        },
        newsMoveDown: function (index) {//下移图文
            if (index >= this.news_list.length - 1){
                utils.showToastr({'status':'error','title':'移动错误','message':'顶级图文无法继续下移'});
                return false;
            }
            var tempOption = this.news_list[index];
            Vue.set(this.news_list, index, this.news_list[index + 1]);
            Vue.set(this.news_list, index + 1, tempOption);
            if (this.news_index == index){
                this.news_index = index + 1;
                this.news_actions_index = index + 1;
            }
        },
        newsEdit:function (index){
            this.news_index = index;
        },
        newsAdd:function () {//添加图文
            if (this.news_list.length >= 8){
                utils.showToastr({'status':'error','title':'添加图文错误','message':'最多只可以加入8条图文消息'});
                return false;
            }
            this.news_list.push(news_empty);
        },
        materialSave:function(){
            /*console.log({material_data:this.material_data, news_list:this.news_list});
            return false;*/
            let loadBox = layer.load(1, {
                shade: [0.3,'#fff'] //0.1透明度的白色背景
            });
            let url_save = this.material_data.material_id ? page_params.url_edit : page_params.url_add;
            utils.ajaxPost(url_save, {material_data:this.material_data, news_list:this.news_list}, (response, data) => {
                layer.close(loadBox);
                utils.showToastr(response.data, 'go');
            });
        },
    },
    components: {
        Treeselect
    },
});